package com.hoshiicloud.market.controller.commission;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.utils.BeanUtils;
import com.hoshiicloud.market.entity.commission.CommissionProfile;
import com.hoshiicloud.market.param.commission.CommissionProfileAddParam;
import com.hoshiicloud.market.param.commission.CommissionProfileQueryParam;
import com.hoshiicloud.market.param.commission.CommissionProfileUpdateParam;
import com.hoshiicloud.market.service.commission.CommissionProfileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @Author:campchen
 * @Date:Created in 上午 10:24 2019/7/25
 * @PACKAGENAME com.hoshiicloud.user.controller.commission
 * @Description
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/market/commission/commission-profile")
@Api(value = "分佣策略", tags = {"分佣策略"})
public class CommissionProfileController {
    @Autowired
    private CommissionProfileService commissionProfileService;


    /**
     * @param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 分页查询佣金配置
     */
    @ApiOperation(value = "分页查询佣金配置")
    @PostMapping("/query/page")
    public Response page(@RequestBody @Valid CommissionProfileQueryParam param) {
        try {
            QueryWrapper<CommissionProfile> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda()
                    .eq((param.getCompanyid() != null && param.getCompanyid() != 0), CommissionProfile::getCompanyid, param.getCompanyid())
                    .eq((param.getPlatformid() != null && param.getPlatformid() != 0), CommissionProfile::getPlatformid, param.getPlatformid())
                    .eq((param.getStoreid() != null && param.getStoreid() != 0), CommissionProfile::getStoreid, param.getStoreid())
                    .eq(StringUtils.isNotBlank(param.getName()), CommissionProfile::getName, param.getName())
                    .eq(StringUtils.isNotBlank(param.getDescription()), CommissionProfile::getDescription, param.getDescription())
                    .eq((param.getEnabled() != null), CommissionProfile::getEnabled, param.getEnabled())
                    .eq(StringUtils.isNotBlank(param.getStrategiesJson()), CommissionProfile::getStrategiesJson, param.getStrategiesJson())
                    .eq((param.getDeleted() != null), CommissionProfile::getDeleted, param.getDeleted())
                    .eq((param.getStatus() != null), CommissionProfile::getStatus, param.getStatus());
            IPage<CommissionProfile> page = new Page<>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));
            String key = param.toString() + ",Page{pageNumber=" + param.getPageNumber() + ",pageSize=" + param.getPageSize() + "}";
            return Response.data(commissionProfileService.getPage(page, queryWrapper, key));
        } catch (Exception e) {
            log.error("分页查询佣金配置异常", e);
            return Response.failed(e.getMessage());
        }

    }

    /**
     * @param id
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 根据id查询佣金配置
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询佣金配置")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response queryById(@PathVariable("id") Long id) {
        return Response.data(commissionProfileService.getById(id));
    }


    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增佣金配置")
    public Response save(@RequestBody @Valid CommissionProfileAddParam param) {
        /**将字符串转化为对象：Strategies strategies = JSONObject.parseObject(param.getStrategies().toString(), Strategies.class);*/
        CommissionProfile data;
        try {
            data = BeanUtils.covertType(param, CommissionProfile.class);
            /**ADD 策略字段与数据库字段不同 将策略内部数据详细展示出来 因此使用对象传输 将对象转化为字符串*/
            data.setStrategiesJson(param.getStrategies().toString());
        } catch (Exception e) {
            log.error("新增佣金配置Bean转换异常", e);
            return Response.failed("新增佣金配置Bean转换异常");
        }
        return Response.data(commissionProfileService.save(data));

    }

    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 修改佣金配置
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改佣金配置")
    public Response update(@RequestBody @Valid CommissionProfileUpdateParam param) {
        CommissionProfile data;
        try {
            data = BeanUtils.covertType(param, CommissionProfile.class);
            /**使内部数据展示清晰*/
            data.setStrategiesJson(param.getStrategies().toString());
        } catch (Exception e) {
            log.error("修改佣金配置Bean转换异常", e);
            return Response.failed("新增佣金配置Bean转换异常");
        }
        return Response.data(commissionProfileService.updateById(data));
    }

}
